import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import { createHtmlPlugin } from 'vite-plugin-html';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import setupExtend from 'vite-plugin-vue-setup-extend';
import * as path from 'path';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    console.log(loadEnv(mode, process.cwd())['VITE_APP_CONTEXT_PATH']);
    return {
        base: loadEnv(mode, process.cwd())['VITE_APP_CONTEXT_PATH'],
        resolve: {
            //设置别名
            alias: {
                '@': path.resolve(__dirname, 'src'),
                '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap')
            }
        },
        plugins: [
            dynamicImportVars({
                include: 'src/pages/'
            }),
            vue(),
            createHtmlPlugin({
                minify: true,
                inject: {
                    data: {
                        title: loadEnv(mode, process.cwd())['VITE_APP_TITLE']
                    }
                }
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/svgs')],
                symbolId: 'icon-[name]'
            }),
            setupExtend()
        ],
        server: {
            open: true,
            host: '0.0.0.0',
            port: 8112, //启动端口
            hmr: {
                port: 8112
            }
            // 设置代理
            // proxy: {
            //     '/api': {
            //         target: 'http://10.10.202.93:8185/', // 测试环境
            //         changeOrigin: true,
            //         rewrite: (path: string) => path.replace(/^\/api/, '')
            //     }
            // }
        }
    };
});
